import os
import shutil

def get_abs_file_path(file_path):
    """判断文件是否存在, 并返回file绝对路径"""
    file_path = os.path.abspath(file_path)
    file_path = file_path.replace("\\", "/")
    if not os.path.isfile(file_path):
        raise Exception("Error! {} is not exist or not a file!".format(file_path))
    return file_path

def get_abs_dir(dir):
    """判断目录是否存在, 并返回目录绝对路径"""
    dir = os.path.abspath(dir)
    dir = dir.replace("\\", "/")
    if not os.path.isdir(dir):
        raise Exception("Error! {} is not exist or not a dir!".format(dir))
    return dir

def create_dir(dir):
    """判断目录是否存在, 若不存在, 创建, 并返回目录绝对路径"""
    dir = os.path.abspath(dir)
    dir = dir.replace("\\", "/")
    os.makedirs(dir, exist_ok=True)
    return dir

def create_save_dir_from_file(file_path):
    """根据file路径判断file所在目录是否存在, 若不存在, 创建, 并返回file所在目录绝对路径"""
    file_path = os.path.abspath(file_path)
    file_path = file_path.replace("\\", "/")
    file_dir = os.path.dirname(file_path)
    os.makedirs(file_dir, exist_ok=True)
    return file_dir

def create_tmp_file_path(file_path):
    """根据file路径创建tmp file路径, 原则是在file文件名前缀后加_tmp, 比如：
       file_path为/home/abc/123.png, 那么创建文件名/home/abc/123_tmp.png, 
       这个函数的主要目的就是创建临时文件，不需要写很长的代码，直接调用该函数即可"""
    file_path = get_abs_file_path(file_path)
    dir_name = os.path.dirname(file_path)
    file_name = os.path.basename(file_path)
    tmp_list = file_name.rsplit(".", 1)
    if len(tmp_list) == 1:
        tmp_file_path = file_path + "_tmp"
    else:
        tmp_file_path = os.path.join(dir_name, tmp_list[0]) + "_tmp." + tmp_list[1]
    return tmp_file_path

def copy_files(src_files, dst_files):
    """src_files和dst_files都为列表, 且个数相同, 并且src_files的每一项对应dst_files的每一项"""
    assert len(src_files) == len(dst_files)
    for i in range(len(src_files)):
        if not os.path.isfile(src_files[i]):
            raise Exception("Error! {} is not exist or not a file!".format(src_files[i]))

        create_save_dir_from_file(dst_files[i])

        shutil.copy(src_files[i], dst_files[i])

def get_row_col_info(file_paths, pattern):
    """file_paths为列表, 包含所有文件的路径;
       pattern为正则表达式, 在路径中查找row和col信息。"""
    row_col_info = [pattern.findall(f)[0] for f in file_paths]
    row_list = sorted(list(set(int(info[0]) for info in row_col_info)))
    col_list = sorted(list(set(int(info[1]) for info in row_col_info)))
    min_row, max_row = row_list[0], row_list[-1]
    min_col, max_col = col_list[0], col_list[-1]
    row_col_dict = {"min_row": min_row, "max_row": max_row, "min_col": min_col, "max_col": max_col}
    return row_col_dict


def main():
    pass

if __name__ == '__main__':
    main()